var viewModel = {
    childMenu : ko.observableArray([]),
    videoSrc : ko.observable(),
};
ko.applyBindings(viewModel);
var artIdList = [];
// get menu
function getMenu(id) {
	var iframeLen = $("#videoDiv").children().filter("iframe").length;
	if (iframeLen == 1) {
		$("iframe").remove();
	}
	var param = {
		menuId : id
	};
	$.ajax({
	    url : "servlet/GetMenuServlet",
	    method : "GET",
	    data : param,
	    dataType : "json",
	    success : function(dataS) {
		    artIdList = [];
		    var len = dataS.length;
		    if (len > 0) {
			    $("#noPartVideo").css("display", "none");
			    viewModel.childMenu.removeAll();
			    $.each(dataS, function(k, v) {
				    viewModel.childMenu.push({
				        childClass : 'titleLi',
				        childId : 'title_' + v.id,
				        childName : v.title,
				        contents : v.contents,
				        childUrl : v.url
				    });
				    artIdList.push('title_' + v.id);
			    });
			    $("#" + viewModel.childMenu()[0].childId).removeClass('titleLi').addClass('active');
			    var str = viewModel.childMenu()[0].contents;
			    $("#preAct").css("visibility", "hidden");
			    $("#nextAct").css("visibility", "visible");
			    $("#articleTab").empty();
			    $("#articleTab").append(str);
			    $("#editor-reader-full").css("padding-right", "0");
			    viewModel.videoSrc(viewModel.childMenu()[0].childUrl);
			    $("#videoImg").css("display", "block");
			    $("#pagerNav").css("display", "block");
			    // show video image
			    var url = viewModel.childMenu()[0].childUrl;
			    if (url.length == 0) {
				    $("#videoImg").css("display", "none");
				    $("#noVideo").css("display", "block");
			    } else {
				    $("#videoImg").css("display", "block");
				    $("#noVideo").css("display", "none");
			    }
		    } else {
			    // if menu is empty
			    viewModel.childMenu.removeAll();
			    viewModel.videoSrc("");
			    $("#articleTab").empty();
			    var str = "<h1>该分类下暂无文章！</h1>";
			    $("#articleTab").append(str);
			    $("#noPartVideo").css("display", "block");
			    $("#noVideo").css("display", "none");
			    $("#videoImg").css("display", "none");
			    $("#pagerNav").css("display", "none");
		    }
	    },
	    error : function(dataE) {
		    console.log(dataE);
	    }
	});
}
getMenu(17);
// 一级菜单
var firstMenu = "menu_17";
$("#scroller>a").on('click', function() {
	var that = this;
	$("#scroller").children('.active').removeClass('active visible-title');
	$("#" + firstMenu).removeClass("firstMenu");
	$(that).addClass('active visible-title');
	// get child menu
	var menuId = that.id;
	var reg = new RegExp("/|_|/");
	var id = menuId.split(reg)[1];
	getMenu(id);
	firstMenu = menuId;
});

//$("#scroller>a").on('mouseenter', function() {
//	var that = this;
//	console.log(that);
//	$("#scroller").children('.active').removeClass("active");
//	$("#scroller").children('.firstMenu').removeClass("firstMenu");
//	$("#" + firstMenu).addClass('firstMenu');
//	$(that).addClass('active');
////	event.stopPropagation();
//});
$("#scroller>a").on('mouseout', function() {
	$("#scroller").children('.active').removeClass('active');
	$("#" + firstMenu).addClass('active');
});
// 二级菜单
$("#secondUl>li").on('click', function() {
	var that = this;
	$("#secondUl").children('.active').removeClass('active');
	$(that).addClass('active');
	// get data
});

// 切换tab
$('#secondUl a').click(function(e) {
	e.preventDefault()
	$(this).tab('show')
})
// change article
function changeArticle() {
	var that = this;
	var id = that.childId;
	$("#leftMenu").children('.active').removeClass('active').addClass('titleLi');
	$("#" + id).addClass('active');
	var idIndex = artIdList.indexOf(id);
	var str = viewModel.childMenu()[idIndex].contents;
	var idLen = artIdList.length;
	if (idIndex == 0) {
		$("#pagerNav").css("display", "block");
		$("#preAct").css("visibility", "hidden");
		$("#nextAct").css("visibility", "visible");
	} else if (idIndex == idLen - 1) {
		$("#pagerNav").css("display", "block");
		$("#preAct").css("visibility", "visible");
		$("#nextAct").css("visibility", "hidden");
	} else {
		$("#pagerNav").css("display", "block");
		$("#preAct").css("visibility", "visible");
		$("#nextAct").css("visibility", "visible");
	}
	$("#articleTab").empty();
	$("#articleTab").append(str);
	$("#editor-reader-full").css("padding-right", "0");
	// change image and video source
	// show video iamge
	var url = viewModel.childMenu()[idIndex].childUrl;
	if (url.length == 0) {
		$("#videoImg").css("display", "none");
		$("#noVideo").css("display", "block");
	} else {
		$("#videoImg").css("display", "block");
		$("#noVideo").css("display", "none");
	}
	var iframeLen = $("#videoDiv").children().filter("iframe").length;
	if (iframeLen == 1) {
		$("iframe").remove();
	}
	$('#back-to-top').tooltip('hide');
	$('body,html').animate({
		scrollTop : 0
	}, 800);
}

// previous article
$("#preAct").on("click", function() {
	var activeId = $("#leftMenu").children('.active').attr("id");
	var idIndex = artIdList.indexOf(activeId);
	var preIndex = idIndex - 1;
	var preId = artIdList[preIndex];
	$("#leftMenu").children('.active').removeClass('active').addClass('titleLi');
	$("#" + preId).addClass('active');
	var str = viewModel.childMenu()[preIndex].contents;
	var idLen = artIdList.length;
	if (preIndex == 0) {
		$("#preAct").css("visibility", "hidden");
		$("#nextAct").css("visibility", "visible");
	} else if (preIndex == idLen - 1) {
		$("#preAct").css("visibility", "visible");
		$("#nextAct").css("visibility", "hidden");
	} else {
		$("#preAct").css("visibility", "visible");
		$("#nextAct").css("visibility", "visible");
	}
	$("#articleTab").empty();
	$("#articleTab").append(str);
	$("#editor-reader-full").css("padding-right", "0");
	// change image and video source
	// show video iamge
	var url = viewModel.childMenu()[idIndex].childUrl;
	if (url.length == 0) {
		$("#videoImg").css("display", "none");
		$("#noVideo").css("display", "block");
	} else {
		$("#videoImg").css("display", "block");
		$("#noVideo").css("display", "none");
	}
	var iframeLen = $("#videoDiv").children().filter("iframe").length;
	if (iframeLen == 1) {
		$("iframe").remove();
	}
	$('#back-to-top').tooltip('hide');
	$('body,html').animate({
		scrollTop : 0
	}, 800);
});
// next article
$("#nextAct").on("click", function() {
	var activeId = $("#leftMenu").children('.active').attr("id");
	var idIndex = artIdList.indexOf(activeId);
	var nextIndex = idIndex + 1;
	var nextId = artIdList[nextIndex];
	$("#leftMenu").children('.active').removeClass('active').addClass('titleLi');
	$("#" + nextId).addClass('active');
	var str = viewModel.childMenu()[nextIndex].contents;
	var idLen = artIdList.length;
	if (nextIndex == 0) {
		$("#preAct").css("visibility", "hidden");
		$("#nextAct").css("visibility", "visible");
	} else if (nextIndex == idLen - 1) {
		$("#preAct").css("visibility", "visible");
		$("#nextAct").css("visibility", "hidden");
	} else {
		$("#preAct").css("visibility", "visible");
		$("#nextAct").css("visibility", "visible");
	}
	$("#articleTab").empty();
	$("#articleTab").append(str);
	$("#editor-reader-full").css("padding-right", "0");
	// change image and video source
	$('#back-to-top').tooltip('hide');
	$('body,html').animate({
		scrollTop : 0
	}, 800);
	// show video iamge
	var url = viewModel.childMenu()[idIndex].childUrl;
	if (url.length == 0) {
		$("#videoImg").css("display", "none");
		$("#noVideo").css("display", "block");
	} else {
		$("#videoImg").css("display", "block");
		$("#noVideo").css("display", "none");
	}
	var iframeLen = $("#videoDiv").children().filter("iframe").length;
	if (iframeLen == 1) {
		$("iframe").remove();
	}
});

// change image
$("#videoImg").on("click", function() {
	$("#videoImg").css("display", "none");
	var children = $("#leftMenu").children();
	if (children.length == 0) {
		$("#noVideo").css("display", "none");
		$("#noPartVideo").css("display", "block");
	} else {
		$("#noPartVideo").css("display", "none");
		var activeLiId = children.filter(".active").attr("id");
		var activeLiIndex = children.index($("#" + activeLiId));
		var url = viewModel.childMenu()[activeLiIndex].childUrl;
		if (url.length == 0) {
			$("#noVideo").css("display", "block");
		} else {
			$("#noVideo").css("display", "none");
			$("#loading").css("display", "block");
			var iframe = document.createElement("iframe");
			iframe.src = url;
			iframe.setAttribute("class", "embed-responsive-item videoIframe");
			if (iframe.attachEvent) {
				iframe.attachEvent("onload", function() {
					$("#loading").css("display", "none");
				});
			} else {
				iframe.onload = function() {
					$("#loading").css("display", "none");
				};
			}
			var videoDiv = document.getElementById("videoDiv");
			videoDiv.appendChild(iframe);
		}
	}
});
$(document).ready(function() {
	$(window).scroll(function() {
		if ($(this).scrollTop() > 200) {
			$('#back-to-top').fadeIn();
			$("#menuDiv").addClass("fixMenu");
		} else {
			$("#menuDiv").removeClass("fixMenu");
			$('#back-to-top').fadeOut();
		}
	});
	// scroll body to 0px on click
	$('#back-to-top').click(function() {
		$('#back-to-top').tooltip('hide');
		$('body,html').animate({
			scrollTop : 0
		}, 800);
		return false;
	});
	$('#back-to-top').tooltip('show');
});
